package timing.ukulele.item.api;

import timing.ukulele.common.ResponseResult;
import timing.ukulele.common.item.ItemData;
import timing.ukulele.item.persistent.Item;
import timing.ukulele.item.service.ItemService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/item")
public class ItemApi {
    private final ItemService itemService;

    public ItemApi(ItemService itemService) {
        this.itemService = itemService;
    }

    @GetMapping("/getByCode")
    @ApiOperation("根据事项编码查询事项")
    public ResponseResult<ItemData> getByCode(
            @ApiParam(value = "事项编号", required = true) @RequestParam("code") String code) {
        final Item item = itemService.getByCode(code);
        if (item != null) {
            ItemData data = new ItemData();
            BeanUtils.copyProperties(item, data);
            return ResponseResult.success(data);
        }
        return ResponseResult.success();
    }

    @GetMapping("/getBySystemId/{systemId}")
    @ApiOperation("根据业务系统获取事项列表")
    public ResponseResult<List<ItemData>> getBySystemId(
            @ApiParam(value = "事项编号", required = true) @PathVariable("systemId") Long systemId) {
        List<ItemData> itemDatas = itemService.getBySystemId(systemId);
        return ResponseResult.success(itemDatas);
    }

    @GetMapping("/formTemplate")
    @ApiOperation("查询绑定某表单的事项")
    public ResponseResult<List<Item>> getItemByFormId(@RequestParam("formTemplateId") Long formTemplateId) {
        List<Item> list = itemService.lambdaQuery().eq(Item::getTemplateId, formTemplateId).list();
        return ResponseResult.success(list);
    }

    @GetMapping("/flowTemplate")
    @ApiOperation("查询绑定某流程的事项")
    public ResponseResult<List<Item>> getItemByFlowId(@RequestParam("flowId") Long flowId) {
        List<Item> list = itemService.lambdaQuery().eq(Item::getFlowId, flowId).list();
        return ResponseResult.success(list);
    }
}
